home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo1.zoo / demo / icon / iconmsgs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-25  |  9.4 KB  |  392 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: iconmsgs.c,v 4.1 88/06/21 13:59:58 bianchi Exp $
  9.     $Source: /fs/16/21272/bianchi/src/nmgr/demo/icon/RCS/iconmsgs.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /fs/16/21272/bianchi/src/nmgr/demo/icon/RCS/iconmsgs.c,v $$Revision: 4.1 $";
  12.  
  13. /* check for new messages */
  14. /* icon version */
  15.  
  16. #include <stdio.h>
  17. #include <signal.h>
  18. #include <sgtty.h>
  19. #include "term.h"
  20. #include "msgs_icons.h"
  21.  
  22. #define Isflag(arg,flag)    (!strncmp(arg,flag,strlen(flag)))
  23. #define Max(x,y)        ((x)>(y)?(x):(y))
  24. #define MENU_COUNT    (sizeof(menu)/sizeof(struct menu_entry))
  25. #define SCMP(x,y)    (strcmp(x+(strlen(x)-strlen(y)),y)==0)
  26.  
  27. static char    MSG_reading[] = "\freading msgs ...\r";
  28. static char    MSG_done[] = "\rdone? [?,-,!]";
  29. static char    MSG_help[] = "\r\
  30. Available commands:\r\n\
  31.  ?*          this message\r\n\
  32.  -           return to most recently seen message\r\n\
  33.  -<number>   skip back <number> messages\r\n\
  34.  !<cmd>      escape to the shell and issue <cmd>\r\n\
  35.  <anything>  exit\r\n";
  36.  
  37. #define W_WIDE        80        /* character width of msgs window */
  38. #define W_HIGH        24        /* character height of msgs window */
  39.  
  40. #define MSGSCMD    "msgs -p"
  41. #define BOUNDS    "/usr/msgs/bounds"
  42. #define RC    ".msgsrc"
  43. #define POLL     30                /* polling interval */
  44. #define XPOS    220                /* start of msgs window */
  45. #define YPOS    170                /* start of msgs window */
  46. #define MAX    50                /* max number of messages */
  47.  
  48. #define MSGS()    (1 + get_bounds(bounds) - get_rc(rc))
  49. #define dprintf    if(debug) fprintf
  50.  
  51. static FILE    *bounds, *rc;        /* pntrs to bounds and rc files */
  52. static int    msg_cnt, old_msg_cnt=0;    /* # of messages */
  53. static char    line[MAXLINE];        /* input buffer */
  54. static int    poll=POLL;        /* poll interval */
  55.  
  56. static struct menu_entry menu[] = {
  57.    "yes",    "y\r",
  58.    "skip",   "n\r",
  59.    "again",  "-\r",
  60.    "save",   "s\r",
  61.    "quit",   "q\r",
  62.    };
  63.  
  64. static int    x, y, w, h;    /* window location, width and height */
  65. static int    border;        /* size of border */
  66. static int    debug = 0;    /* debug printout flag */
  67. static int    local=0;    /* use local machine for icons */
  68. static int    cwide, chigh;    /* width and height of font characters. */
  69. static int my_win_id = 0;    /* my window id */
  70. static char    *termcap;
  71.  
  72. main(argc,argv)
  73. int argc;
  74. char **argv;
  75.    {
  76.    register int i;
  77.    int xpos = XPOS;
  78.    int ypos = YPOS;
  79.    int font = -1;
  80.    int shape = 1;
  81.  
  82.    char *getenv();
  83.    char *home = getenv("HOME");
  84.    int clean(), update();
  85.  
  86.    /* make sure we have a valid environment to run in */
  87.  
  88.    ckmgrterm( *argv );
  89.  
  90.    if (home==NULL || *home=='\0') {
  91.       fprintf(stderr,"%s: Can't find your home directory\n",argv[0]);
  92.       exit(1);
  93.       }
  94.  
  95.    if ((bounds = fopen(BOUNDS,"r")) == NULL) {
  96.       fprintf(stderr,"%s: Can't find a bounds file\n",argv[0]);
  97.       exit(2);
  98.       }
  99.  
  100.    sprintf(line,"%s/%s",home,RC);
  101.    
  102.    if ((rc = fopen(line,"r")) == NULL) {
  103.       fprintf(stderr,"%s: Can't find %s\n",argv[0],line);
  104.       exit(3);
  105.       }
  106.  
  107.    /* process arguments */
  108.  
  109.    for(i=1;i<argc;i++) {
  110.       if (Isflag(argv[i],"-s"))
  111.          shape = 0;
  112.       else if (Isflag(argv[i],"-x"))
  113.          xpos = atoi(argv[i]+2);
  114.       else if (Isflag(argv[i],"-y"))
  115.          ypos = atoi(argv[i]+2);
  116.       else if (Isflag(argv[i],"-f"))
  117.          font = atoi(argv[i]+2);
  118.       else if (Isflag(argv[i],"-p"))
  119.          poll  = Max(atoi(argv[i]+2),10);
  120.       else if (Isflag(argv[i],"-d"))
  121.          debug=1;
  122.       else if (Isflag(argv[i],"-l"))
  123.          local=1;
  124.       else
  125.          usage(argv[0],argv[i]);
  126.       }
  127.  
  128.    /* setup mgr stuff */
  129.  
  130.    m_setup(M_FLUSH);
  131.    m_push(P_BITMAP|P_MENU|P_EVENT|P_FONT|P_FLAGS|P_POSITION);
  132.    if (font < 0)
  133.     font = 0;
  134.    m_font(font);
  135.    get_font( &cwide, &chigh );
  136.     
  137.    signal(SIGHUP,clean);
  138.    signal(SIGTERM,clean);
  139.    signal(SIGINT,clean);
  140.    signal(SIGALRM,update);
  141.  
  142.    m_ttyset();
  143.    m_setmode(M_NOWRAP);
  144.    m_setmode(M_ABS);
  145.    m_func(B_COPY);
  146.  
  147.     /* get window id */
  148.  
  149.     m_getinfo(G_ID);
  150.    m_gets(line);
  151.     my_win_id = atoi(line);
  152.  
  153.    download_icon(&msg_board,1);
  154.    download_icon(&msg_note,2);
  155.    download_icon(&msg_read,3);
  156.    download_icon(&msg_none,4);
  157.  
  158.    get_size(&x,&y,&w,&h);
  159.    get_param(0,0,0,&border);
  160.    m_movecursor(x+30,0);
  161.  
  162.    old_msg_cnt = MSGS();
  163.    if (shape && !debug) {
  164.       m_shapewindow(x,y,2*border+msg_board.w,2*border+msg_board.h);
  165.       }
  166.    else if (debug)
  167.       fprintf(stderr,"would shape (%d,%d) + %d\r\n",
  168.                msg_board.w, msg_board.h, 2*border);
  169.    m_setevent(REDRAW,"R\r");
  170.    m_setevent(ACTIVATED,"A\r");
  171.    update(0);
  172.    m_clearmode(M_ACTIVATE);
  173.  
  174.    termcap = getenv("TERMCAP");
  175.    if( termcap )
  176.       *termcap = '\0';
  177.  
  178.    while(1) {
  179.       if( m_gets(line) == NULL ) {
  180.      clearerr(m_termin);
  181.      continue;
  182.       }
  183.       alarm(0);
  184.  
  185.       /* read msgs */
  186.  
  187.       old_msg_cnt = msg_cnt;
  188.       msg_cnt = MSGS();
  189.       if (msg_cnt > 0 && *line == 'A') {
  190.          do_msgs(MSGSCMD,font,xpos,ypos);
  191.          }
  192.  
  193.       /* wait for window to deactivate */
  194.  
  195.       else if (*line == 'A') {
  196.          set_icon(msg_none,0,0);
  197.          sleep(2);
  198.          }
  199.       m_clearmode(M_ACTIVATE);
  200.       update(0);
  201.       }
  202.    }
  203.     
  204. int
  205. get_rc(file)
  206. FILE *file;
  207.    {
  208.    char line[100], *fgets();
  209.    fseek(file,0,0);
  210.    if (fgets(line,sizeof(line),file) != NULL) 
  211.       return(atoi(line));
  212.    else
  213.       return(0);
  214.    }
  215.  
  216. int
  217. get_bounds(file)
  218. FILE *file;
  219.    {
  220.    char buff[100], *line, *fgets();
  221.    fseek(file,0,0);
  222.    if ((line=fgets(buff,sizeof(buff),file)) != NULL) {
  223.       while(*line != ' ') line++;
  224.       while(*line == ' ') line++;
  225.       return(atoi(line));
  226.       }
  227.    else return(0);
  228.    }
  229.  
  230. clean(n)
  231. int n;
  232.    {
  233.    m_ttyreset();
  234.    m_selectwin(my_win_id);
  235.    m_popall();
  236.    exit(n);
  237.    }
  238.  
  239. update(flag)
  240. int flag;
  241.    {
  242.    alarm(0);
  243.    msg_cnt = MSGS();
  244.    if (msg_cnt != old_msg_cnt || flag==0) {
  245.       if (msg_cnt > old_msg_cnt)         /* new messages */
  246.          m_printstr("\007");
  247.       draw(msg_cnt,old_msg_cnt,flag==0);
  248.       }
  249.    old_msg_cnt = msg_cnt;
  250.    alarm(poll);
  251.    }
  252.  
  253. usage(name,error)
  254. char *name, *error;
  255. {
  256.     fprintf(stderr,"Invalid argument: %s\n",error);
  257.     fprintf(stderr,"usage: %s -[s|x<pos>|y<pos>|f<font>|p<poll>\n",name);
  258.     exit(1);
  259. }
  260.  
  261. /* down load an icon */
  262.  
  263. download_icon(icon,where)
  264. register struct icon *icon;    /* name of icon to download */
  265. int where;            /* bitmap to download icon to */
  266.    {
  267.    static int local_mode = -1;
  268.    int size;
  269.    int w_in=0, h_in=0;
  270.  
  271.    /* first try the local machine */
  272.  
  273.    if (!local) {
  274.       dprintf(stderr, "looking for %s\n", icon->name);
  275.       m_bitfile(where, icon->name , &w_in, &h_in);
  276.       }
  277.  
  278.    if (h_in==0 || w_in==0) {    /* can't find icon */
  279.       dprintf(stderr,"Couldn't find %s, downloading\n",icon->name);
  280.       if (local_mode == -1)
  281.          ioctl(fileno(m_termout),TIOCLGET,&local_mode);
  282.       local_mode |= LLITOUT;
  283.       ioctl(fileno(m_termout),TIOCLSET,&local_mode);
  284.  
  285.       size = icon->h * (((icon->w+15)&~15)>>3);
  286.       m_bitldto(icon->w,icon->h,0,0,where,size);
  287.       m_flush();
  288.       write(fileno(m_termout),icon->data,size);
  289.       local_mode &= ~LLITOUT;
  290.       ioctl(fileno(m_termout),TIOCLSET,&local_mode);
  291.       }
  292.    else {
  293.       dprintf(stderr,"Found %s (%d x %d) expected %d x %d\n",
  294.                icon->name,w_in,h_in,icon->w,icon->h);
  295.       icon->w = w_in;
  296.       icon->h = h_in;
  297.       }
  298.    icon->type = where;
  299.    } 
  300.  
  301. #define Rand(min,max)    ((min) + random()%((max)-(min)))
  302. #define MAXNOTES    100        /* enough to make the message board
  303.                     look filled, but not take too long
  304.                     to draw when there are hundreds of
  305.                     messages. */
  306.  
  307. draw(new,old,redraw)
  308. int new,old;                /* number of messages */
  309. int redraw;
  310.    {
  311.    register int i;
  312.    long random();
  313.  
  314.    dprintf(stderr,"draw %d -> %d (redraw=%d)\n",old,new,redraw);
  315.  
  316.    if (redraw || new<old)
  317.       set_icon(msg_board,0,0);
  318.  
  319.    if (new>old)
  320.       new -= old;
  321.  
  322.    for(i=0;  i<new && i<MAXNOTES;  i++) {
  323.       set_icon( msg_note, Rand(0, msg_board.w-msg_note.w),
  324.         Rand(12, msg_board.w-msg_note.h) );
  325.       }
  326.    }
  327.    
  328. set_icon(name,x0,y0)
  329. struct icon name;        /* name of icon */
  330. int x0,y0;            /* where it goes */
  331.    {
  332.    m_bitcopyto(x0,y0,name.w,name.h,0,0,0,name.type);
  333.    m_flush();
  334.    }
  335.    
  336. /* run msgs in a subwindow */
  337.  
  338. do_msgs(command,font,xpos,ypos)
  339. char *command;
  340. int font,xpos,ypos;
  341.     {
  342.     int n;
  343.  
  344.     alarm(0);
  345.     m_push(P_EVENT | P_FONT);
  346.     n = m_makewindow(xpos, ypos, W_WIDE*cwide + 2*border,
  347.         W_HIGH*chigh + 2*border );
  348.     if (n==0) {    /* can't make window */
  349.         m_printstr("\007\fCan't open msgs window, sorry");
  350.         m_pop();
  351.         return(0);
  352.         }
  353.       set_icon(msg_read, 0, 0);
  354.     m_selectwin(n);
  355.     m_font(font);
  356.     menu_load(1,MENU_COUNT,menu);
  357.     m_selectmenu(1);
  358.     m_printstr(MSG_reading);
  359.     m_ttyreset();
  360.     system(command);
  361.     m_printstr(MSG_done);
  362.         while( m_gets(line) != NULL ) {
  363.         switch( *line ) {
  364.         case '?':
  365.             m_printstr(MSG_help);
  366.             break;
  367.         case '!':    /* shell escape */
  368.             system( &line[1] );
  369.             break;
  370.         case '-': {    /* go back; -N goes back N messages */
  371.                 char    buf[ 2*MAXLINE ];
  372.                 if( line[1] == '\n' )
  373.                     strcpy( line, "-1" );
  374.                 sprintf( buf, "%s %s", command, line );
  375.                 system( buf );
  376.             }
  377.             break;
  378.         default:
  379.             goto nomore;        /* bianchi, how could you do such a horrible thing
  380.                                         to my code????? */
  381.         }
  382.         m_printstr(MSG_done);
  383.     }
  384.     clearerr(m_termin);
  385.     nomore:
  386.     m_ttyset();
  387.     m_selectwin(my_win_id);
  388.     m_destroywin(n);
  389.     m_pop();
  390.     m_clearmode(M_ACTIVATE);
  391.     }
  392.